package com.simas.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

//import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

//import net.rim.device.api.servicebook.ServiceBook;
//import net.rim.device.api.servicebook.ServiceRecord;
//import net.rim.device.api.system.CoverageInfo;
//import net.rim.device.api.system.WLANInfo;

import com.simas.screen.XmlShowINFOScreen;

public class NoWaitProcess extends Thread {
	private String url;
	private String method; // GET or POST
	private XmlShowINFOScreen screen1;
	private CommonConnection comCon = new CommonConnection();
	
//	private ServiceRecord getWAP2ServiceRecord() {
//		ServiceBook sb = ServiceBook.getSB();
//		ServiceRecord[] records = sb.getRecords();
//		for(int i = 0; i < records.length; i++) {
//			String cid = records[i].getCid().toLowerCase();
//			String uid = records[i].getUid().toLowerCase();
//			if (cid.indexOf("wptcp") != -1 &&
//					uid.indexOf("wifi") == -1 &&
//					uid.indexOf("mms") == -1) {
//					return records[i];
//			}
//		}
//		return null;
//	}
	
	public NoWaitProcess(String url, String method, XmlShowINFOScreen screen) {
		this.url = url;
		this.method = method;
		this.screen1 = screen;
	}
	
	public void run() {
		try {
			
//			/** tambahan baru **/
//			String connectionParameters = "";
//			if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
//				// Connected to a WiFi access point
//				System.out.println("Connected to a WiFi access point");
//				connectionParameters = ";interface=wifi";
//			} else {
//				int coverageStatus = CoverageInfo.getCoverageStatus();
//				ServiceRecord record = getWAP2ServiceRecord();
//				if (record != null && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
//					// Have network coverage and a WAP 2.0 service book record
//					System.out.println("Have network coverage and a WAP 2.0 service book record");
//					connectionParameters = ";deviceside=true;ConnectionUID=" + record.getUid();
//				} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
//					// Have an MDS service book and network coverage
//					System.out.println("Have an MDS service book and network coverage");
//					connectionParameters = ";deviceside=false";
//				} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
//					// Have network coverage but no WAP 2.0 service book record
//					System.out.println("Have network coverage but no WAP 2.0 service book record");
//					connectionParameters = ";deviceside=true";
//				}
//			}
//			HttpConnection connection = (HttpConnection)Connector.open(url + connectionParameters);
//			/** tambahan baru **/			
			
//			HttpConnection connection = (HttpConnection)Connector.open(url);
			
			HttpConnection connection = comCon.getConnection(url);
			connection.setRequestMethod(method);
			
			int responseCode = connection.getResponseCode();
			if (responseCode != HttpConnection.HTTP_OK) {
				screen1.requestFailed("Unexpected response code: " + responseCode);
				connection.close();
				return;
			}
			
			String contentType = connection.getHeaderField("Content-type");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			InputStream responseData = connection.openInputStream();
			byte[] buffer = new byte[10000];
			int bytesRead = responseData.read(buffer);
			while(bytesRead > 0) {
				baos.write(buffer, 0, bytesRead);
				bytesRead = responseData.read(buffer);
			}
			baos.close();
			connection.close();
			screen1.imageRetrieved(baos.toByteArray(), contentType);
		}
		catch (IOException ex) {
			screen1.requestFailed(ex.toString());
		}
	}
}
